// pages/test/anchor/anchor.js
//var base64=require('../../../utils/base64.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selected:'',
    popupState:false,
    settingState:false,
    playState:true,
    beautyValue:1,
    whitenessValue:1,
    pushLive:null,
    chatArr: [],
    giftsArr:[],
    onlineNum:0,
    onlinePerson: [{ id: 1001, pic: 'img/ren.jpg', nickname: '隔壁马蓉', age: 32, sex: 0, area: '福建 厦门', follow_num: 107, fans_num: 9820, gifts_num: 323 }, { id: 1002, pic: 'img/ren.jpg', nickname: '插口袋的白百何', age: 33, sex: 1, area: '广东 宏远', follow_num: 2015, fans_num: 15020, gifts_num: 3512 }, { id: 1003, pic: 'img/ren.jpg', nickname: '爱做头发的李小璐', age: 28, sex: 0, area: '北京 首钢', follow_num: 4967, fans_num: 4121, gifts_num: 562 }, { id: 1004, pic: 'img/ren.jpg', nickname: '邻居张子萱', age: 12, sex: 0, area: '湖南 长沙', follow_num: 432, fans_num: 4214, gifts_num: 26 }, { id: 1005, pic: 'img/ren.jpg', nickname: '据说还有的马苏', age: 43, sex: 0, area: '安徽 合肥', follow_num: 152, fans_num: 1521, gifts_num: 326 }, { id: 1006, pic: 'img/ren.jpg', nickname: '大嘴姚晨', age: 48, sex: 1, area: '新疆 乌鲁木齐', follow_num: 46, fans_num: 1213, gifts_num: 54 }, { id: 1007, pic: 'img/ren.jpg', nickname: '爱小鲜肉的伊能静', age: 32, sex: 0, area: '山东 鲁能', follow_num: 120, fans_num: 484, gifts_num: 41 }, { id: 1008, pic: 'img/ren.jpg', nickname: '老牛吃嫩草的邓文迪', age: 56, sex: 1, area: '德国 柏林', follow_num: 523, fans_num: 84, gifts_num: 5231}]
  },
  //开启个人信息弹窗
  openPopup: function (event) {
    // console.log(event.currentTarget.dataset.info);
    // var that=this;
    // this.setData({
    //   selected: event.currentTarget.dataset.info,
    //   popupState: true
    // });
  },
  //关闭个人信息弹窗
  closePopup:function(){
    this.setData({
      popupState: false
    });
  },
  //阻值冒泡
  stopProgration:function(){

  },
  //查看在线人
  goOnlinePeople:function(){
    wx.navigateTo({
      url: '../onlinePeople/onlinePeople'
    })
  },
  //打开设置
  openSet:function(){
    var that = this;
    this.setData({
      settingState:true
    });    
  },
  //关闭设置
  closeSet:function(){
    this.setData({
      settingState: false
    });
  },
  //发红包
  goRedPackage:function(){
    var that=this;
    wx.navigateTo({
      url: '../redPackage/redPackage?id='+that.data.roomId
    })
  },
  //切换镜头
  changeCamera:function(){
    this.pusher.switchCamera({
      success: function () {
        console.log('switch success!');
      },
      fail: function () {
        console.log('switch failed!')
      },
      complete: function () {
        console.log('switch complete!')
      }
    });
  },
  //美白
  adjustMB:function(){
    var num = this.data.whitenessValue;
    num++;
    num = num > 9 ? 1 : num;
    this.setData({ whitenessValue : num});
  },
  //美颜
  adjustMY: function () {
    var num = this.data.beautyValue;
    num++;
    num = num > 9 ? 1 : num;
    this.setData({ beautyValue: num });
  },
  //暂停或开始
  startOrpause: function () {
    var that = this;
    if (this.data.playState) {
      this.pusher.pause({
        success: function () {
          console.log('pause success!');
          that.setData({
            playState:false,
          });
        },
        fail: function () {
          console.log('pause failed!');
        },
        complete: function () {
          console.log('pause complete!');
        }
      });
    } else {
      this.pusher.resume({
        success: function () {
          console.log('start success!');
          that.setData({
            playState: true
          });
        },
        fail: function () {
          console.log('start failed!');
        },
        complete: function () {
          console.log('start complete!');
        }
      });
    }
  },
  goback:function(){
    this.pusher.stop({
      success: function () {
        console.log('pause success!');
      },
      fail: function () {
        console.log('pause failed!');
      },
      complete: function () {
        console.log('pause complete!');
      }
    });
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;
    me.setData({ pushLive: decodeURI(options.push_flow_url) +'?vhost=live.sencegroup.com', roomId: options.id});
    wx.connectSocket({
      url: 'wss://wx.sencegroup.com/xingchen/wchat/weixin/' + options.id+'/'+app.globalData.token
    });
    wx.onSocketOpen(function (res) {
      console.log('WebSocket连接已打开！');
      // wx.sendSocketMessage({});
    });
    wx.onSocketError(function (res) {
      console.log('WebSocket连接打开失败，请检查！')
    });
    wx.onSocketMessage(function (res) {
      console.log('收到服务器内容：' + res.data);
      let obj = JSON.parse(res.data);
      console.log(obj);
      switch (obj.cmd) {
        case ("chatCaches"):
          me.setData({chatArr: obj.data});
          break;
        case ("chat"):
          let chatBox = me.data.chatArr;
          chatBox.unshift(obj.data);
          me.setData({chatArr:chatBox});
          break;
        case ("out"):
          wx.showToast({
            title: obj.data,
            icon: 'cancel',
            duration: 2000
          })
          break;
        case ("redpacket_wait"):
          let redBox = me.data.giftsArr;
          redBox.unshift(obj.data);
          me.setData({ giftsArr: redBox });
          break;
        case ("gift"):
          let giftsBox = me.data.giftsArr;
          giftsBox.unshift(obj.data);
          me.setData({ giftsArr: giftsBox });
          break;
        case ("online_user"):
          me.setData({
            onlineNum: obj.data.onlineSum,
            onlinePerson: obj.data.onlineTop
          });
          break;
      }
      console.log(me.data.chatArr);
    });
    wx.onSocketClose(function (res) {
      console.log('WebSocket 已关闭！')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获取推流框
    this.pusher = wx.createLivePusherContext('pusher');
    console.log('wss://wx.sencegroup.com/xingchen/wchat/weixin/'+app.globalData.liveId+'/'+app.globalData.token);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '星辰直播间',
      path: 'https://wx.sencegroup.com/pages/live.html?liveId=15',
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: '分享成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: '分享失败',
          icon: 'cancel',
          duration: 2000
        })
      }
    }
  }
})